import { getBaseUrl } from '@/services/urlconst';
import { requestFunComponFiex } from '@/utils';
import {
  getCompanyID,
  getPosIDs,
  getUserDepID,
  getUserID,
} from '@/utils/authority';
import { useRequest } from 'umi';

let baseUrl = getBaseUrl();

/**
 * 编辑首页布局
 * @param editLayoutCall 接口调用后的回调
 * @returns
 */
const editLayoutService = (editLayoutCall: (res: any) => void) => {
  const editLayout = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/commonController/editLayout`;
      let methodType = 'PUT';
      return requestFunComponFiex(methodType, reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: editLayoutCall,
    },
  );
  return {
    editLayoutRun: editLayout?.run,
    editLayoutLoading: editLayout?.loading,
  };
};

/**
 * 新增首页布局
 * @param addLayoutCall 接口调用后的回调
 * @returns
 */
const addLayoutService = (addLayoutCall: (res: any) => void) => {
  const addLayout = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        userID: getUserID(),
      };
      let reqUrl = `${baseUrl}/commonController/addLayout`;
      let methodType = 'POST';
      return requestFunComponFiex(methodType, reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: addLayoutCall,
    },
  );
  return {
    addLayoutRun: addLayout?.run,
    addLayoutLoading: addLayout?.loading,
  };
};

/**
 * 新增首页布局
 * @param editLayoutCall 接口调用后的回调
 * @returns
 */
const resetLayoutService = (resetLayoutCall: (res: any) => void) => {
  const resetLayout = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        userID: getUserID(),
      };
      let reqUrl = `${baseUrl}/commonController/resetLayout`;
      let methodType = 'PUT';
      return requestFunComponFiex(methodType, reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: resetLayoutCall,
    },
  );
  return {
    resetLayoutRun: resetLayout?.run,
    resetLayoutLoading: resetLayout?.loading,
  };
};

export { editLayoutService, addLayoutService, resetLayoutService };
